package com.portme.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.portme.client.pages.AccountPage;
import com.portme.client.pages.Homepage;
import com.portme.shared.LoginInfo;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Portme implements EntryPoint, ValueChangeHandler<String> {
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Add history listener
		
		String currentToken = History.getToken();
		if(currentToken.length() == 0) {
			History.newItem("home");
		}
		History.addValueChangeHandler(this);
		// Now that we've setup our listener, fire the initial history state.
	    History.fireCurrentHistoryState();
	    
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		if(event.getValue() != null) {
			
			if(event.getValue().equals("home")) {
				RootPanel.get("main").clear();
				
				PortmeServiceClient.getInstance().getLoginUrl(new AsyncCallback<LoginInfo>() {
					
					@Override
					public void onSuccess(LoginInfo result) {
						StaticFactory.getInstance().setCurrentUserLogin(result);
						Homepage home = new Homepage();
						RootPanel.get("main").add(home);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert("There is something wrong with the connection. Please try again!");
					}
				});
				
			} else if(event.getValue().equals("account")) {
				RootPanel.get("main").clear();
				PortmeServiceClient.getInstance().getLoginUrl(new AsyncCallback<LoginInfo>() {
					
					@Override
					public void onSuccess(LoginInfo result) {
						StaticFactory.getInstance().setCurrentUserLogin(result);
						AccountPage account = new AccountPage(result.getUsername().split("@")[0]+"");
						RootPanel.get("main").add(account);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert("There is something wrong with the connection. Please try again!");
					}
				});
			} else {
				RootPanel.get("main").clear();
				final String username = event.getValue();
				PortmeServiceClient.getInstance().getLoginUrl(new AsyncCallback<LoginInfo>() {
					
					@Override
					public void onSuccess(LoginInfo result) {
						StaticFactory.getInstance().setCurrentUserLogin(result);
//						UserPortfolio user = new UserPortfolio(username);
						AccountPage user = new AccountPage(username);
						RootPanel.get("main").add(user);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert("No profile found for "+username);
					}
				});
			}
		}
	}
}
